package com.timesheet.server.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.timesheet.server.common.models.EffortTrackerDTO;
import com.timesheet.server.core.mapper.Mapper3;
import com.timesheet.server.core.models.EffortTracker;
import com.timesheet.server.core.util.HibernateUtil;
import com.timesheet.server.core.util.PMManager;

public class EffortTrackerDao {

	public boolean fillEfforts(EffortTrackerDTO effortTrackerDto)
			throws Exception {

		EffortTracker effortTracker = new EffortTracker();


		EffortTracker effortTracker2 = Mapper3.map(effortTracker,
				effortTrackerDto);

		return PMManager.persist(effortTracker2);

	}

	public List<EffortTrackerDTO> getDailyEffortsForAWeekend(String empId,
			String projId, Date weekDate) {

		List<EffortTracker> wsrList = PMManager.getDailzEffortsForAWeekend(
				empId, projId, weekDate);
		List<EffortTrackerDTO> dto = new ArrayList<EffortTrackerDTO>();
		for (EffortTracker effortTracker : wsrList) {
			EffortTrackerDTO tmpDto = new EffortTrackerDTO();
			EffortTrackerDTO map = Mapper3.map(tmpDto, effortTracker);
			System.out.println("map.getStatus(): " + map.getStatus());
			dto.add(map);
		}
		return dto;

	}

	public boolean deleteEffort(EffortTrackerDTO effortTrackerDto)
			throws Exception {

		EffortTracker effortTracker = Mapper3.map(new EffortTracker(),
				effortTrackerDto);
		return PMManager.delete(effortTracker);
	}

	public List<EffortTrackerDTO> getAllEffortsInBetweenDates(Date fromDate, Date toDate) {
		return null ;
	//	return toDate;

	}

	public List<EffortTrackerDTO> getWSR(String empId, String projId,
			Date weekDate) {
		List<EffortTrackerDTO> dtoList = new ArrayList<EffortTrackerDTO>();
		List wsrValues = PMManager.getWSR(empId, projId, weekDate);
		for (int i = 0; i < wsrValues.size(); i++) {
			Object object[] = (Object[]) wsrValues.get(i);
			if (object != null && object.length > 3) {
				EffortTrackerDTO effortTrackerDTO = new EffortTrackerDTO();
				effortTrackerDTO.setTaskType(object[0].toString());
				effortTrackerDTO.setTaskNo(object[1].toString());
				effortTrackerDTO.setHours(new Double(object[2].toString()));
				effortTrackerDTO.setTaskDescription(object[3].toString());
				dtoList.add(effortTrackerDTO);
			}
		}
		return dtoList;
	}

	public boolean updateEffortTracker(EffortTrackerDTO dto) {
		EffortTracker effortTrackerEntity = new EffortTracker();
		EffortTracker effortTracker = Mapper3.map(effortTrackerEntity, dto);
		return PMManager.update(effortTracker);

	}
	
	
public static List<EffortTracker> getAllEfforts(String[] projects) {		
		if(projects==null & projects.length==0){
			return Collections.emptyList();
		}
		List<EffortTracker> list = new ArrayList<EffortTracker>();
		HibernateUtil hibenateUtil = new HibernateUtil();
		Session session = hibenateUtil.getSession();
		for (String string : projects) {
			String sql = "SELECT * FROM EffortTracker eff where eff.projectId='"+string+"'";
			SQLQuery query = session.createSQLQuery(sql);
			query.addEntity(EffortTracker.class);			
			list.addAll(query.list());
		}
		return list;

	}
}
